package com.example.student_manager_backend.controller;

import com.example.student_manager_backend.status.StudentStatus;
import com.example.student_manager_backend.common.Result;
import com.example.student_manager_backend.dto.StudentDTO;
import com.example.student_manager_backend.entity.Student;
import com.example.student_manager_backend.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "学生管理", description = "学生信息管理接口")
@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Operation(summary = "创建学生")
    @PostMapping
    public Result<Student> createStudent(@Valid @RequestBody StudentDTO studentDTO) {
        Student student = convertToEntity(studentDTO);
        return Result.success(studentService.createStudent(student));
    }

    @Operation(summary = "更新学生信息")
    @PutMapping("/{studentId}")
    public Result<Student> updateStudent(@PathVariable String studentId, @Valid @RequestBody StudentDTO studentDTO) {
        Student student = convertToEntity(studentDTO);
        return Result.success(studentService.updateStudent(studentId, student));
    }

    @Operation(summary = "删除学生")
    @DeleteMapping("/{studentId}")
    public Result<Void> deleteStudent(@PathVariable String studentId) {
        studentService.deleteStudent(studentId);
        return Result.success();
    }

    @Operation(summary = "获取学生信息")
    @GetMapping("/{studentId}")
    public Result<Student> getStudent(@PathVariable String studentId) {
        return Result.success(studentService.getStudent(studentId));
    }

    @Operation(summary = "分页查询学生列表")
    @GetMapping
    public Result<Page<Student>> findStudents(Specification<Student> spec, Pageable pageable) {
        return Result.success(studentService.findStudents(spec, pageable));
    }

    @Operation(summary = "根据班级查询学生")
    @GetMapping("/class/{classId}")
    public Result<List<Student>> findStudentsByClass(@PathVariable String classId) {
        return Result.success(studentService.findStudentsByClass(classId));
    }

    @Operation(summary = "导入学生数据")
    @PostMapping("/import")
    public Result<Void> importStudents(@RequestParam("file") MultipartFile file) {
        studentService.importStudents(file);
        return Result.success();
    }

    @Operation(summary = "导出学生数据")
    @GetMapping("/export")
    public void exportStudents() {
        ArrayList students = new ArrayList<Student>();
        studentService.exportStudents(students);
    }

    private Student convertToEntity(StudentDTO dto) {
        Student student = new Student();
        student.setStudentId(dto.getStudentId());
        student.setName(dto.getName());
        student.setGender(dto.getGender());
        student.setBirthday(dto.getBirthday());
        student.setClassId(dto.getClassId());
        student.setPhone(dto.getPhone());
        student.setEmail(dto.getEmail());
        student.setStatus(StudentStatus.valueOf(dto.getStatus()));
        return student;
    }
} 